<template>
    <div>
        <div>
            <el-col :span="6">
                <div class="grid-content bg-purple"></div>
                <el-button type="primary" @click="onhandAdd">添加超级会员种类</el-button>
            </el-col>

        </div>
        <br>
        <br>
        <br>
        <div>
            <el-col :span="6">
                <div class="grid-content bg-purple"></div>
                <el-popconfirm confirm-button-text='确认' cancel-button-text='取消' icon="el-icon-info" icon-color="red"
                               title="确认删除所有选中的会员种类吗？" @confirm="deleteSelected">
                    <el-button type="danger" slot="reference"  v-bind:disabled="disable">删除所选</el-button>
                </el-popconfirm>
            </el-col>
        </div>
        <el-table ref="multipleTable" :data="tableData" stripe style="width: 100%" tooltip-effect="dark"
                  @selection-change="handleSelectionChange">
            <el-table-column
                    prop="vipKindId"
                    type="selection"
                    width="55">

            </el-table-column>
            <el-table-column prop="vipKindName" label="会员种类名称" width="180">
            </el-table-column>
            <el-table-column prop="vipKindPrice" label="价格" width="180">
            </el-table-column>
            <el-table-column prop="vipKindZhekoulv" label="折扣率" width="180">
            </el-table-column>
            <el-table-column prop="vipKindImg" label="封面" width="250">
                <template v-slot="scope">
                <div class="demo-image__preview" >
                    <el-image
                            style="width: 100px; height: 100px;"
                            :src="scope.row.vipKindImg"
                            :preview-src-list="[scope.row.vipKindImg,]">
                        <div slot="error" class="image-slot">
                            <i class="el-icon-picture-outline"></i>尚未添加封面，请编辑
                        </div>
                    </el-image>
                </div></template>
            </el-table-column>
            <el-table-column label="操作" prop="vipKindId">
                <template v-slot="scope">
                    <el-button type="primary" @click="handleEdit(scope.row.vipKindId)">编辑
                    </el-button>
                    <el-popconfirm confirm-button-text='确认' cancel-button-text='取消' icon="el-icon-info" icon-color="red"
                                   title="确认删除此会员种类吗？" @confirm="del(scope.row.vipKindId)">
                        <el-button type="danger" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin-top: 20px">
            <el-button @click="toggleSelection()">取消选择</el-button>
        </div>
        <!-- 分页 -->
        <div style="margin-top: 10px;">
            <el-pagination background :current-page="params.pageNum" :page-size="params.pageSize"
                           layout="prev, pager, next" :total="total" @current-change="handleCurrentChange">
            </el-pagination>
        </div>
        <!-- 新增编辑弹框 -->
        <el-dialog :title="title" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
            <el-form :inline="true" :model="vipkindform" status-icon :rules="rules" ref="vipkindform"
                     style="width: 80%;"
                     label-width="200px">
                <el-form-item label="会员种类名称" prop="vipKindName">
                    <el-input v-model="vipkindform.vipKindName" placeholder="请输入会员种类名称"></el-input>
                </el-form-item>
                <el-form-item label="价格" prop="vipKindPrice">
                    <el-input v-model="vipkindform.vipKindPrice" placeholder="请输入价格"></el-input>
                </el-form-item>
                <el-form-item label="折扣率" prop="vipKindZhekoulv">
                    <el-input v-model="vipkindform.vipKindZhekoulv" placeholder="请输入折扣率"></el-input>
                </el-form-item>
                <el-form-item label="商城封面" prop="vipKindImg">
                <el-upload
                        style="text-align: left;margin-left: 40px;margin-bottom: 20px"
                        class="avatar-uploader"
                        action="http://localhost:8081/file/upload/"
                        :before-upload="beforeAvatarUpload"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess">
                    <img v-if="vipkindform.vipKindImg" :src="vipkindform.vipKindImg" class="avatar" style="object-fit: cover">
                    <i style="border: 1px solid gray" v-else class="el-icon-plus avatar-uploader-icon">上传商城封面</i>
                </el-upload>
                </el-form-item>
            </el-form>
            <div style="text-align-last: center;width: 60%;">
                <el-button @click="closedialog('vipkindform')" type="danger">取 消</el-button>
                <el-button @click="resetForm('vipkindform')" type="warning">重置</el-button>
                <el-button style="margin-right: 30px;" type="primary" icon="el-icon-upload2 "
                           @click="dialogVisibleConfirm('vipkindform')" size="medium">{{but}}
                </el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import request from '@/request'

    export default {
        name: 'VipKind',
        data() {
            var reg = /^(0.\d+|0|1)$/;
            var checkZhekoulv = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('请输入折扣率'));
                }
                setTimeout(() => {
                    if (!reg.test(value)) {
                        callback(new Error('折扣率不能超过0~1'));
                    } else {
                        callback();
                    }
                }, 500);
            };
            return {
                //选中的id存全局
                multipleSelection: [],
                tableData: [],
                total: 0,
                allid: "",
                title: "",
                but:"",
                disable:true,
                dialogVisible: false,
                params: {
                    pageNum: 1,
                    pageSize: 8,
                    receptionAdminName:'',
                    receptionAdminNum:''
                },
                vipkindform: {
                    vipKindName: '',
                    vipKindZhekoulv: '',
                    vipKindPrice: '',
                    vipKindImg:'',
                },

                rules: {
                    vipKindName: [{
                        required: true,
                        message: '会员种类名称不能为空',
                        trigger: 'blur'
                    },],
                    vipKindPrice: [{
                        required: true,
                        message: '价格不能为空',
                        trigger: 'blur'
                    },],
                    vipKindZhekoulv: [{
                        required: true,
                        validator: checkZhekoulv,
                        trigger: 'blur'
                    }]

                }
            }
        },
        mounted() {
            this.load()
        },
        methods: {
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            handleSelectionChange(val) {
                if (val.length==0){
                    this.multipleSelection.length=val.length
                    this.disable=true
                }else {
                    this.disable=false
                    for (let i = 0; i < val.length ;i++) {
                        this.multipleSelection[i]=val[i].vipKindId
                    }
                 }
            },
            add(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        request.post('/vipkind/add', this.vipkindform).then(res => {
                            if (res.code === '200') {
                                this.$notify.success('新增成功')
                                this.resetForm(formName) //清空表格数据
                                this.dialogVisible = false; //关闭弹框
                                this.load(); //刷新列表
                            } else {
                                this.$notify.error(res.msg)
                            }
                        })
                    }
                });
            },
            update(formName) {
                request.put('/vipkind/update', this.vipkindform).then(res => {
                    if (res.code === '200') {
                        this.$notify.success('修改成功')
                        this.resetForm(formName) //清空表格数据
                        this.dialogVisible = false; //关闭弹框
                        this.load(); //刷新列表
                    } else {
                        this.$notify.error(res.msg)
                    }
                })
            },
            del(vipKindId) {
                request.delete('/vipkind/delete' + vipKindId).then(res => {
                    if (res.code === '200') {
                        this.$notify.success('删除成功')
                        this.load();
                    } else {
                        this.$notify.error(res.msg)
                    }
                })

            },
            deleteSelected(){
                console.log(this.multipleSelection)
                request.post('/vipkind/deletes', this.multipleSelection).then(res => {
                    console.log(res)
                    if (res.code === '200') {
                        this.$notify.success('批量删除成功')
                        this.load(); //刷新列表
                    } else {
                        this.$notify.error(res.msg)
                    }
                })
            },
            handleClose(done) {
                this.dialogVisible = false;
            },
            onhandAdd() {
                this.but='添加'
                this.title = '添加超级会员种类';
                this.resetForm('vipkindform');
                this.dialogVisible = true;
            },
            handleEdit(vipKindId) {
                this.but='修改'
                this.title = '修改超级会员种类'
                this.dialogVisible = true; //显示弹框
                request.get("/vipkind/" + vipKindId).then(res => {
                    this.vipkindform = res.data
                })//row当前行数据，把当前行的数据赋值给 表单
                this.allid = vipKindId; //把id存全局
            },

            //清空表单
            resetForm(formName) {
                this.$nextTick(() => {
                    this.$refs[formName].resetFields();
                })
            },
            //页面加载
            load() {
                request.get('/vipkind/page', {
                    params: this.params
                }).then(res => {
                    console.log(res)
                    if (res.code === "200") {
                        this.total = res.data.total;
                        this.tableData = res.data.list;
                    }
                })
            },


            //核心判断哪一个按钮调用表单组件
            dialogVisibleConfirm(formName) {
                // 新增接口
                if (!this.allid) {
                    this.add(formName)

                } else {
                    // 修改接口
                    this.update(formName);
                }
            },

            closedialog(formName) {
                this.dialogVisible = false;
                this.resetForm(formName) //清空表格数据
            },

            handleCurrentChange(pageNum) {
                this.params.pageNum = pageNum;
                this.load()
            },
            //监测图片格式和大小是否合适
            beforeAvatarUpload(file) {
                const isJPEG = file.type === 'image/jpeg';
                const isJPG = file.type === 'image/jpg';
                const isPNG = file.type === 'image/png';
                const isLt5M = file.size / 1024 / 1024 < 5;

                if (!(isJPG || isJPEG || isPNG)) {
                    this.$message.error('上传头像图片只能是 JPG,PNG,JPEG 格式!');
                }
                if (!isLt5M) {
                    this.$message.error('上传头像图片大小不能超过 5MB!');
                }
                return (isJPG || isJPEG || isPNG) && isLt5M;
            },
            //头像上传
            handleAvatarSuccess(res) {
                this.vipkindform.vipKindImg = res;
            },
        }
    }
</script>
<style scoped>

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9 !important;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 24px;
        color: #8c939d;
        width: 200px;
        height: 250px;
        line-height: 250px;
        text-align: center;
    }

    .avatar {
        width: 200px;
        height: 250px;
        display: block;
    }

</style>
